package com.push.xu.xpush.receiver;


import android.content.Context;

import com.push.xu.xpush.constant.Constant;
import com.push.xu.xpush.util.PushRepeater;
import com.push.xu.xpush.util.SPUtils;
import com.xiaomi.mipush.sdk.ErrorCode;
import com.xiaomi.mipush.sdk.MiPushClient;
import com.xiaomi.mipush.sdk.MiPushCommandMessage;
import com.xiaomi.mipush.sdk.MiPushMessage;
import com.xiaomi.mipush.sdk.PushMessageReceiver;

import java.util.List;

public class MiuiReceiver extends PushMessageReceiver {

	private String mRegId;
    private String mTopic;
    private String mAccount;
    private String mStartTime;
    private String mEndTime;

    @Override
    public void onReceivePassThroughMessage(Context context, MiPushMessage message) {
        PushRepeater.transmitMessage(context, message.getDescription(), message.getContent(), message.getExtra());
    }

    @Override
    public void onNotificationMessageClicked(Context context, MiPushMessage message) {
    	PushRepeater.transmitNotificationClick(context,message.getNotifyId(),message.getTitle(),message.getDescription(), message.getContent(), message.getExtra());
    }

    @Override
    public void onNotificationMessageArrived(Context context, MiPushMessage message) {
        PushRepeater.transmitNotification(context,message.getNotifyId(),message.getTitle(),message.getDescription(),message.getContent(), message.getExtra());
    }

    @Override
    public void onCommandResult(Context context, MiPushCommandMessage message) {

        String command = message.getCommand();
        List<String> arguments = message.getCommandArguments();
        String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments.get(0) : null);
        String cmdArg2 = ((arguments != null && arguments.size() > 1) ? arguments.get(1) : null);

        if (MiPushClient.COMMAND_REGISTER.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mRegId = cmdArg1;
            } 
        }

    }

    @Override
    public void onReceiveRegisterResult(Context context, MiPushCommandMessage message) {

        String command = message.getCommand();
        List<String> arguments = message.getCommandArguments();
        String cmdArg1 = ((arguments != null && arguments.size() > 0) ? arguments.get(0) : null);
        PushRepeater.transmitCommandResult(context, cmdArg1,null,message.getReason());

        //保存这个token
        SPUtils.getInstance(Constant.PUSH_MESSAGE_TABLE).put("push_token", cmdArg1);
        if (MiPushClient.COMMAND_REGISTER.equals(command)) {
            if (message.getResultCode() == ErrorCode.SUCCESS) {
                mRegId = cmdArg1;
            }
        }
    }

}

